package com.odianyun.hello.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.odianyun.db.mybatis.EntityQueryParam;
import com.odianyun.db.mybatis.QueryParam;
import com.odianyun.db.query.PageVO;
import com.odianyun.hello.mapper.CodeMapper;
import com.odianyun.hello.service.CodeService;
import com.odianyun.hello.model.PO.CodePO;
import com.odianyun.hello.model.VO.CodeVO;
import com.odianyun.project.base.IEntity;
import com.odianyun.project.query.PageQueryArgs;
import com.odianyun.project.query.QueryArgs;
import com.odianyun.project.support.base.db.EQ;
import com.odianyun.project.support.base.db.Q;
import com.odianyun.project.support.base.service.OdyEntityService;

import java.beans.Beans;
import java.util.*;
import java.util.stream.Collectors;
import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

@Service
public class CodeServiceImpl extends OdyEntityService<CodePO, CodeVO, PageQueryArgs, QueryArgs, CodeMapper> implements CodeService {
    @Resource
    private CodeMapper mapper;
    @Resource
    private  CodeService codeService;
    @Resource
    private RedisTemplate redisTemplate;
    public static final String CODE_INFO_ORDERS="codeinfo";
    public static final String CODE_INFO_PACKAGE="codeinfo";
    public static final String CODE_INFO_ITEM="codeinfo";
    @Override
    protected CodeMapper getMapper() {
        return mapper;
    }

    @Override
	protected void afterListPage(PageVO<? extends IEntity> pageVO) {
	    for (IEntity entity : pageVO.getList()) {
	        CodeVO vo = (CodeVO) entity;
            vo.setPool("pool");
            vo.setCategory("category");
            vo.setCode("code");
            vo.setLanguage("language");
            vo.setSort(0);
	    }
	}
    public List<CodeVO> getStatus() {
        String key = CODE_INFO_ORDERS;
        ValueOperations operations = redisTemplate.opsForValue();
        List<CodeVO> codeListCache = (List<CodeVO>) operations.get(key);
        if (codeListCache != null){
            return codeListCache;
        }
        EntityQueryParam code = new EQ(CodePO.class).withResultClass(CodeVO.class);
        code.eq("category", "ORDER_STATUS");
        code.eq("pool", "default");
        code.eq("language", "zh_CN");
        code.select("code");
        code.select("name");
        List<CodeVO> codeList = codeService.list(code);
        if (!codeList.isEmpty()) {
            operations.set(key,codeList);
        }
        return codeList;
    }
    public Map<String,String> getOrderStatus() {

        EntityQueryParam code = new EQ(CodePO.class).withResultClass(CodeVO.class);
        code.eq("category", "ORDER_STATUS");
        code.eq("pool", "default");
        code.eq("language", "zh_CN");
        code.select("code");
        code.select("name");
        List<CodeVO> map = codeService.list(code);
        Map<String,String> mapStatus = map.stream().collect(Collectors.toMap(CodeVO::getCode , CodeVO::getName));
        System.out.println(JSONObject.toJSONString(mapStatus));
        return mapStatus;
    }

    public Integer getOrderCodeByName(String name){
        String key = CODE_INFO_ORDERS + name;
        ValueOperations operations = redisTemplate.opsForValue();
        Integer sCodeCache = (Integer) operations.get(key);
        if(null != sCodeCache){
            return sCodeCache;
        }
        EntityQueryParam soCode = new EQ(CodePO.class);
        soCode.eq("category", "ORDER_STATUS");
        soCode.eq("language", "zh_CN");
        soCode.eq("pool","default");
        soCode.eq("name",name);
        soCode.select("code");
        CodePO orderStatusCode = mapper.get(soCode);
        Integer sCode = Integer.parseInt(orderStatusCode.getCode());
        if(null != sCode){
            operations.set(key,sCode);
        }
        return sCode;
    }
    public Integer getPackageCodeByName(String name){
        String key = CODE_INFO_PACKAGE + name;
        ValueOperations operations = redisTemplate.opsForValue();
        Integer PackageCodeCache = (Integer) operations.get(key);
        if(null != PackageCodeCache){
            return PackageCodeCache;
        }
        EntityQueryParam packageCode = new EQ(CodePO.class);
        packageCode.eq("category", "PACKAGE_STATUS");
        packageCode.eq("language", "zh_CN");
        packageCode.eq("pool","oms");
        packageCode.eq("name",name);
        packageCode.select("code");
        CodePO packageStatusCode = mapper.get(packageCode);
        Integer pCode = Integer.parseInt(packageStatusCode.getCode());
        if(null != pCode){
            operations.set(key,pCode);
        }
        return pCode;
    }
    public Integer getSoItemCodeByName(String name){
        String key = CODE_INFO_ITEM + name;
        ValueOperations operations = redisTemplate.opsForValue();
        Integer siCodeCache = (Integer) operations.get(key);
        if(null != siCodeCache){
            return siCodeCache;
        }


        EntityQueryParam soItemCode = new EQ(CodePO.class);
        soItemCode.eq("category", "ITEM_STATUS");
        soItemCode.eq("language", "zh_CN");
        soItemCode.eq("pool","oms");
        soItemCode.eq("name",name);
        soItemCode.select("code");
        CodePO soItemStatusCode = mapper.get(soItemCode);
        Integer soiCode = Integer.parseInt(soItemStatusCode.getCode());
        if(null != soiCode){
            operations.set(key,soiCode);
        }
        return soiCode;
    }

    @Override
    public List<CodeVO> listByCategory(String pool, String category) {
        QueryParam code = new Q("code","name");
        code.eq("pool", pool);
        code.eq("language","zh_CN");
        code.eq("category" , category);
        List<CodeVO> codeVOList = codeService.list(code);
        return codeVOList;
    }

}
